package com.dropgame.world;

import java.util.ArrayList;
import java.util.Random;
import java.util.TimerTask;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class DropTimerTask extends TimerTask{
	
	ArrayList<Body> circleArray;
	ArrayList<BodyStore> bodyStore;
	World world;
	MasterDrop masterDrop;
	
	CircleShape circleShape4;
	BodyDef circleBodyDef4;
	Body circleBody4;
	
	Random randomGenerator;
	
	int type;
	int type2;
	int ranPos;
	int level;
	float worldPos;
	float worldPos2;
	
	
	public DropTimerTask(ArrayList<BodyStore> bodyStore, World world, MasterDrop masterDrop, int level){
		this.bodyStore = bodyStore;
		this.world = world;
		this.masterDrop = masterDrop;
		this.level = level;
		randomGenerator = new Random();
		circleShape4 = new CircleShape();
		circleShape4.setRadius(1.4f);
		
		circleBodyDef4 = new BodyDef();
		circleBodyDef4.type = BodyType.DynamicBody;
		circleBodyDef4.position.x = 3;
		circleBodyDef4.position.y = 25;
	}
	
	public void run() {
	      System.out.println("The drop timer ran ");
	      //if(world.isLocked())
	      
	    	  //masterDrop.addNewDrop();
	    	  
	      
	      	//System.out.println("The dropwasent added");
	      	 
		  //circleShape4.dispose();
	      worldPos2 = createWorldPos();
	      type2 = createType(level);
	      
	      System.out.println(worldPos2);
	      if(world.isLocked()) return;
	      circleBodyDef4.position.x = worldPos2;
	      circleBodyDef4.position.y = 48;
	    	circleBody4 = world.createBody(circleBodyDef4);
			circleBody4.createFixture(circleShape4, 10);
	    	if(bodyStore.isEmpty()){
	    		bodyStore.add(new BodyStore(circleBody4, 0, type));
	    		return;
	    	}
	    	int len = bodyStore.size();
	    	for(int i = 0; i < len; i ++){
	    		if(bodyStore.get(i) == null){
	    			bodyStore.set(i, new BodyStore(circleBody4, i, type));
	    			return;
	    		}
	    	}
	    	
	    	bodyStore.add(new BodyStore(circleBody4, len, type));
	    	return;
	    }
	
	public float createWorldPos(){
		ranPos = randomGenerator.nextInt(320);
		float nonMod = randomGenerator.nextFloat();
	    worldPos = ranPos / 10;
	    return worldPos + nonMod;
	    
	}
	
	public int createType(int level){
		type = randomGenerator.nextInt(level);
		return type;
	}
}
